package com.jerry.frame.rpc.registry;

/**
 * Created by zjh on 2017/8/7.
 */
//注册信息
public class RegistryMeta {

    //地址
    private Address address;
    //metadata
    private ServiceMeta serviceMeta;
    //权重
    private volatile int weight;

    //建议连接数
    private volatile  int connCount;

    public RegistryMeta(Address address, ServiceMeta serviceMeta, int weight, int connCount) {
        this.address = address;
        this.serviceMeta = serviceMeta;
        this.weight = weight;
        this.connCount = connCount;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        RegistryMeta that = (RegistryMeta) o;

        if (address != null ? !address.equals(that.address) : that.address != null) return false;
        return serviceMeta != null ? serviceMeta.equals(that.serviceMeta) : that.serviceMeta == null;

    }

    @Override
    public int hashCode() {
        int result = address != null ? address.hashCode() : 0;
        result = 31 * result + (serviceMeta != null ? serviceMeta.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "RegistryMeta{" +
                "address=" + address +
                ", serviceMeta=" + serviceMeta +
                ", weight=" + weight +
                ", connCount=" + connCount +
                '}';
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public ServiceMeta getServiceMeta() {
        return serviceMeta;
    }

    public void setServiceMeta(ServiceMeta serviceMeta) {
        this.serviceMeta = serviceMeta;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public int getConnCount() {
        return connCount;
    }

    public void setConnCount(int connCount) {
        this.connCount = connCount;
    }

    public static class Address {

        private String host;

        private int port;

        public Address(String host, int port) {
            this.host = host;
            this.port = port;
        }

        public String getHost() {
            return host;
        }

        public void setHost(String host) {
            this.host = host;
        }

        public int getPort() {
            return port;
        }

        public void setPort(int port) {
            this.port = port;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            Address address = (Address) o;

            if (port != address.port) return false;
            return host != null ? host.equals(address.host) : address.host == null;

        }

        @Override
        public int hashCode() {
            int result = host != null ? host.hashCode() : 0;
            result = 31 * result + port;
            return result;
        }

        @Override
        public String toString() {
            return "Address{" +
                    "host='" + host + '\'' +
                    ", port=" + port +
                    '}';
        }
    }

    //服务元数据
    public static class ServiceMeta {
        //组别
        private String group;
        //服务名
        private String serviceProviderName;
        //版本信息
        private String version;

        public ServiceMeta(String group, String serviceProviderName, String version) {
            this.group = group;
            this.serviceProviderName = serviceProviderName;
            this.version = version;
        }

        public String getGroup() {
            return group;
        }

        public void setGroup(String group) {
            this.group = group;
        }

        public String getServiceProviderName() {
            return serviceProviderName;
        }

        public void setServiceProviderName(String serviceProviderName) {
            this.serviceProviderName = serviceProviderName;
        }

        public String getVersion() {
            return version;
        }

        public void setVersion(String version) {
            this.version = version;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            ServiceMeta that = (ServiceMeta) o;

            if (group != null ? !group.equals(that.group) : that.group != null) return false;
            if (serviceProviderName != null ? !serviceProviderName.equals(that.serviceProviderName) : that.serviceProviderName != null)
                return false;
            return version != null ? version.equals(that.version) : that.version == null;

        }

        @Override
        public int hashCode() {
            int result = group != null ? group.hashCode() : 0;
            result = 31 * result + (serviceProviderName != null ? serviceProviderName.hashCode() : 0);
            result = 31 * result + (version != null ? version.hashCode() : 0);
            return result;
        }

        @Override
        public String toString() {
            return "ServiceMeta{" +
                    "group='" + group + '\'' +
                    ", serviceProviderName='" + serviceProviderName + '\'' +
                    ", version='" + version + '\'' +
                    '}';
        }
    }
}
